package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * TODO
 *
 * @Author mr.wu
 * @Data #{DATE} #{TIME}
 */
@RestController
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @PostMapping()
    public Result<Dish> add(@RequestBody DishDTO dish) {
        if (dishService.add(dish) != 0) {
            return Result.success();
        }
        return Result.error("新增菜品失败");
    }

    @GetMapping("/page")
    public Result<PageResult> getPage(DishPageQueryDTO dto) {
        PageResult list = dishService.getPage(dto);
        return Result.success(list);
    }

    @DeleteMapping()
    @ApiOperation("删除菜品")
    public Result delete(@RequestParam List<Integer> ids) {
        dishService.delete(ids);
        return Result.success();
    }

    @PutMapping()
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        dishService.update(dishDTO);
        return Result.success();
    }

    /**
     * 修改菜品信息根据id回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        DishVO dish = dishService.getById(id);
        return Result.success(dish);
    }

    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status, @RequestParam Long id) {
        int i = dishService.updateStatus(id, status);
        if (i < 1) {
            return Result.error("修改状态失败");
        }
        return Result.success();

    }

    @GetMapping("/list")
    public Result<List<Dish>> getDish(@RequestParam Long categoryId){
        List<Dish> dish = dishService.getDish(categoryId);
        return Result.success(dish);
    }
}
